<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class MConfiguration {

	function MConfiguration () {
		$this->__construct();
	}
	
	function __construct () {
		$CI =& get_instance();
		$CI->load->model('Configurationmodel');
	}
	
	function form ($group, $object_id, $render = true) {
		
		$CI =& get_instance();
		$CI->Configurationmodel->group = $group;
		$CI->Configurationmodel->object_id = $object_id;
		
		$configs = $CI->Configurationmodel->getObjectConfigs();
		
		$inputs = array();
		
		$form = array('inputs' => array());
		
		while ($config = current($configs)) {
			
			$form['inputs'][$config->name] = array(
					'required' => ((int)$config->required > 1), 
					'section' => $config->section, 
					'section_title' => $config->section_title, 
					'section_comment' => $config->section_comment);
			
			if ($render) {
				
				if (!empty($CI->messages['form'])) {
					$errors = $CI->messages['form']['error'];
					$inputs = $CI->messages['form']['inputs'];
				} else {
					$errors = array();
					$inputs = array();
				}
				
				if (!empty($inputs[$config->name])) { $config->value = $inputs[$config->name]; }
				
				$value = $config->value ? $config->value : $config->default_value;
				switch ($config->form_control) {
					case 2:
						$form['inputs'][$config->name]['input'] = form_textarea($config->name, $value);
						break;
					case 3:
						$form['inputs'][$config->name]['input'] = form_checkbox($config->name, $value); 
						break;
					case 4:
						break;
					case 5: 
						$pairs = explode(';', $config->values);
						foreach($pairs as $pair) { 
							list($key, $value) = explode(':', trim($pair));
							$options[trim($key)] = trim($value);
						}
						$form['inputs'][$config->name]['input'] = form_dropdown($config->name, $options, $value);
						break;
					default: //1
						$form['inputs'][$config->name]['input'] = form_input($config->name, $value);
				}
				
				$form['inputs'][$config->name]['label'] = form_label($config->title, $config->name);
				$form['inputs'][$config->name]['tip'] = $CI->lang->line('configuration_' . $config->name . '_tip');
				if (in_array($config->name, $errors)) { $form['inputs'][$config->name]['error'] = $CI->lang->line('configuration_error_msg_common'); }
			}
			next($configs);
		}
		return $form;
	}
	
	function submitConfigs ($group, $object_id) {
		
	}
}
?>
